home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / illustrt / illustrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  9.5 KB  |  335 lines

  1. /*****************************************************************************
  2. * Program to create illustrations of wireframe drawings.             *
  3. *   Usually the output of this program is piped to irit2ps, although it      *
  4. * creates a regular IRIT data files with polylines.                 *
  5. *                                         *
  6. * Written by:  Gershon Elber                Ver 1.0, June 1993   *
  7. *****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include "program.h"
  13. #include "iritgrap.h"
  14. #include "allocate.h"
  15. #include "poly_cln.h"
  16. #include "getarg.h"
  17. #include "ip_cnvrt.h"
  18.  
  19. #ifdef NO_CONCAT_STR
  20. static char *VersionStr =
  21.     "Illustrt        Version 4.0,    Gershon Elber,\n\
  22.      (C) Copyright 1989/90/91/92/93 Gershon Elber, Non commercial use only.";
  23. #else
  24. static char *VersionStr =
  25.     "Illustrt        " VERSION ",    Gershon Elber,    "
  26.     __DATE__ ",   " __TIME__ "\n" COPYRIGHT ", Non commercial use only.";
  27. #endif /* NO_CONCAT_STR */
  28.  
  29. static char *CtrlStr =
  30.     "Illustrt I%-#IsoLines!d S%-#SampPerCrv!d s%- M%- P%- p%- l%-MaxLnLen!F a%- t%-TrimInter!F o%-OutName!s T%- z%- DFiles!*s";
  31.  
  32. static char
  33.     *GlblOutFileName = "illustrt.dat";
  34.  
  35. static int
  36.     GlblTalkative = FALSE,
  37.     GlblSortOutput = FALSE,
  38.     GlblDrawSurfaceMesh = FALSE,
  39.     GlblDrawSurface = TRUE,
  40.     GlblNumOfIsolines = IG_DEFAULT_NUM_OF_ISOLINES,
  41.     GlblSamplesPerCurve = IG_DEFAULT_SAMPLES_PER_CURVE;
  42.  
  43. static RealType
  44.     GlblMaxLineLen = DEFAULT_MAX_LINE_LEN;
  45.  
  46. int GlblAngularDistance = TRUE,
  47.     GlblVertexPoints = FALSE,
  48.     GlblSplitLongLines = FALSE;
  49.  
  50. RealType
  51.     GlblTrimIntersect = DEFAULT_TRIM_INTERSECT;
  52.  
  53. static MatrixType CrntViewMat;            /* This is the current view! */
  54.  
  55. static void MapObjects(IPObjectStruct *PObjects, MatrixType Mat);
  56. static void DumpData(char *FileName,
  57.              IPObjectStruct *NoProcessObjs,
  58.              IPObjectStruct *PObjects);
  59.  
  60. /*****************************************************************************
  61. * Main routine - Read Parameter    line and do what you need...             *
  62. *****************************************************************************/
  63. void main(int argc, char **argv)
  64. {
  65.     int Error,
  66.     NumOfIsolinesFlag = FALSE,
  67.     SamplesPerCurveFlag = FALSE,
  68.         TrimInterFlag = FALSE,
  69.     OutFileFlag = FALSE,
  70.     VerFlag = FALSE,
  71.     NumFiles = 0;
  72.     char
  73.     **FileNames = NULL;
  74.     IPObjectStruct *PObjects, *NoProcessObjs, *PObj;
  75.  
  76.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  77.                &NumOfIsolinesFlag, &GlblNumOfIsolines,
  78.                &SamplesPerCurveFlag, &GlblSamplesPerCurve,
  79.                &GlblSortOutput, &GlblDrawSurfaceMesh,
  80.                &GlblDrawSurface, &GlblVertexPoints,
  81.                &GlblSplitLongLines, &GlblMaxLineLen,
  82.                &GlblAngularDistance,
  83.                &TrimInterFlag, &GlblTrimIntersect,
  84.                &OutFileFlag, &GlblOutFileName, &GlblTalkative,
  85.                &VerFlag, &NumFiles, &FileNames)) != 0) {
  86.     GAPrintErrMsg(Error);
  87.     GAPrintHowTo(CtrlStr);
  88.     IllustrateExit(1);
  89.     }
  90.  
  91.     if (VerFlag) {
  92.     fprintf(stderr, "\n%s\n\n", VersionStr);
  93.     GAPrintHowTo(CtrlStr);
  94.     IllustrateExit(0);
  95.     }
  96.  
  97.     if (!NumFiles) {
  98.     fprintf(stderr, "No data file names where given, exit.\n");
  99.     GAPrintHowTo(CtrlStr);
  100.     IllustrateExit(1);
  101.     }
  102.  
  103.     /* Get the data files: */
  104.     if ((PObjects = IritPrsrGetDataFiles(FileNames, NumFiles, TRUE, FALSE)) ==
  105.                                     NULL)
  106.     IllustrateExit(0);
  107.  
  108.     if (IritPrsrWasPrspMat)
  109.     MatMultTwo4by4(CrntViewMat, IritPrsrViewMat, IritPrsrPrspMat);
  110.     else
  111.     GEN_COPY(CrntViewMat, IritPrsrViewMat, sizeof(MatrixType));
  112.  
  113.     MapObjects(PObjects, CrntViewMat);
  114.  
  115.     for (PObj = PObjects, PObjects = NoProcessObjs = NULL; PObj != NULL; ) {
  116.     IPObjectStruct
  117.         *PObjNext = PObj -> Pnext;
  118.  
  119.     if (AttrGetObjectStrAttrib(PObj, "IllustrtNoProcess") != NULL) {
  120.         PObj -> Pnext = NoProcessObjs;
  121.         NoProcessObjs = PObj;
  122.     }
  123.     else {
  124.         PObj -> Pnext = PObjects;
  125.         PObjects = PObj;
  126.     }
  127.  
  128.     PObj = PObjNext;
  129.     }
  130.  
  131.     if (GlblVertexPoints) {
  132.     /* Add vertices of each polyline as points into data set. */
  133.     for (PObj = PObjects; PObj != NULL;) {
  134.         if (IP_IS_POLY_OBJ(PObj)) {
  135.         IPObjectStruct
  136.             *PtList = CopyObject(NULL, PObj, TRUE);
  137.  
  138.         IP_SET_POINTLIST_OBJ(PtList);
  139.  
  140.         RemoveInternalVertices(PtList);
  141.  
  142.         PObj -> Pnext = PtList;
  143.         PObj = PtList -> Pnext;
  144.         }
  145.         else
  146.         PObj = PObj -> Pnext;
  147.     }
  148.     }
  149.  
  150.     ProcessIntersections(PObjects);
  151.     if (GlblSplitLongLines)
  152.     SplitLongLines(PObjects, GlblMaxLineLen);
  153.  
  154.     if (GlblSortOutput)
  155.     SortOutput(&PObjects);
  156.  
  157.     DumpData(OutFileFlag ? GlblOutFileName : NULL, NoProcessObjs, PObjects);
  158.  
  159.     IllustrateExit(0);
  160. }
  161.  
  162. /*****************************************************************************
  163. * Routine to convert all surfaces/curves into polylines as follows:         *
  164. * Curves are converted to single polyline with SamplesPerCurve samples.         *
  165. * Surface are converted into GlblNumOfIsolines curves in each axes, each     *
  166. * handled as Curves above. The polylines are saved in the appropriate        *
  167. * surface/curve slots.                                 *
  168. *****************************************************************************/
  169. IPObjectStruct *IritPrsrProcessFreeForm(IPObjectStruct *CrvObjs,
  170.                     IPObjectStruct *SrfObjs)
  171. {
  172.     CagdCrvStruct *Crv, *Crvs;
  173.     CagdSrfStruct *Srf, *Srfs;
  174.     IPObjectStruct *PObj;
  175.     IPPolygonStruct *PPolygon, *PPolygonTemp;
  176.  
  177.     if (CrvObjs == NULL && SrfObjs == NULL)
  178.     return NULL;
  179.  
  180.     /* Make sure requested format is something reasonable. */
  181.     if (GlblNumOfIsolines < 2) {
  182.     GlblNumOfIsolines = 2;
  183.     fprintf(stderr, "NumOfIsolines is less than 2, 2 picked instead.\n");
  184.     }
  185.  
  186.     if (GlblSamplesPerCurve < 1) {
  187.     GlblSamplesPerCurve = 1;
  188.     fprintf(stderr,
  189.         "SamplesPerCurve is less than 1, 1 picked instead.\n");
  190.     }
  191.     if (GlblSamplesPerCurve > CAGD_MAX_BEZIER_CACHE_ORDER) {
  192.     GlblSamplesPerCurve = CAGD_MAX_BEZIER_CACHE_ORDER;
  193.     fprintf(stderr,
  194.         "Log2 SamplesPerCurve is more than %d, %d picked instead.\n",
  195.         CAGD_MAX_BEZIER_CACHE_ORDER, CAGD_MAX_BEZIER_CACHE_ORDER);
  196.     }
  197.     BzrCrvSetCache(GlblSamplesPerCurve, TRUE); /* Set up bezier cache. */
  198.  
  199.     if (CrvObjs) {
  200.     for (PObj = CrvObjs; PObj != NULL; PObj = PObj -> Pnext) {
  201.         if (GlblTalkative)
  202.         fprintf(stderr, "Processing curve object \"%s\"\n",
  203.             PObj -> Name);
  204.  
  205.         Crvs = PObj -> U.Crvs;
  206.         PObj -> U.Pl = NULL;
  207.         PObj -> ObjType = IP_OBJ_POLY;
  208.         IP_SET_POLYLINE_OBJ(PObj);
  209.         for (Crv = Crvs; Crv != NULL; Crv = Crv -> Pnext) {
  210.         PPolygon = PPolygonTemp = IritCurve2Polylines(Crv,
  211.                                GlblSamplesPerCurve);
  212.         while (PPolygonTemp -> Pnext)
  213.             PPolygonTemp = PPolygonTemp -> Pnext;
  214.         PPolygonTemp -> Pnext = PObj -> U.Pl;
  215.         PObj -> U.Pl = PPolygon;
  216.         }
  217.         CagdCrvFreeList(Crvs);
  218.     }
  219.     }
  220.  
  221.     if (SrfObjs) {
  222.     int NumOfIso[2];
  223.  
  224.     NumOfIso[0] = -GlblNumOfIsolines;
  225.     NumOfIso[1] = -GlblNumOfIsolines;
  226.  
  227.     for (PObj = SrfObjs; PObj != NULL; PObj = PObj -> Pnext) {
  228.         if (GlblTalkative)
  229.         fprintf(stderr, "Processing surface object \"%s\"\n",
  230.             PObj -> Name);
  231.         
  232.         Srfs = PObj -> U.Srfs;
  233.         PObj -> U.Pl = NULL;
  234.         PObj -> ObjType = IP_OBJ_POLY;
  235.         IP_SET_POLYLINE_OBJ(PObj);
  236.         for (Srf = Srfs; Srf != NULL; Srf = Srf -> Pnext) {
  237.         PPolygon = PPolygonTemp =
  238.             IritSurface2Polylines(Srf, NumOfIso,
  239.                       GlblSamplesPerCurve);
  240.         while (PPolygonTemp -> Pnext)
  241.             PPolygonTemp = PPolygonTemp -> Pnext;
  242.         PPolygonTemp -> Pnext = PObj -> U.Pl;
  243.         PObj -> U.Pl = PPolygon;
  244.         }
  245.         CagdSrfFreeList(Srfs);
  246.     }
  247.     }
  248.  
  249.     if (SrfObjs == NULL)
  250.     return CrvObjs;
  251.     else if (CrvObjs == NULL)
  252.     return SrfObjs;
  253.     else {
  254.     for (PObj = SrfObjs; PObj -> Pnext != NULL; PObj = PObj -> Pnext);
  255.     PObj -> Pnext = CrvObjs;
  256.     return SrfObjs;
  257.     }
  258. }
  259.  
  260. /*****************************************************************************
  261. * Maps the objects according to given transformation matrix.             *
  262. * Only polylines are expected at this time, other objects are ignored.         *
  263. *****************************************************************************/
  264. static void MapObjects(IPObjectStruct *PObjects, MatrixType Mat)
  265. {
  266.     IPObjectStruct *PObj;
  267.  
  268.     for (PObj = PObjects; PObj != NULL; PObj = PObj -> Pnext) {
  269.     if (IP_IS_POLY_OBJ(PObj)) {
  270.         IPPolygonStruct *PPoly;
  271.  
  272.         for (PPoly = PObj -> U.Pl; PPoly != NULL; PPoly = PPoly -> Pnext) {
  273.         IPVertexStruct *V;
  274.  
  275.         for (V = PPoly -> PVertex; V != NULL; V = V -> Pnext)
  276.             MatMultVecby4by4(V -> Coord, V -> Coord, Mat);
  277.         }
  278.     }
  279.     }
  280. }
  281.  
  282. /*****************************************************************************
  283. * Dumps the data out into FileName (stdout in NULL).                 *
  284. *****************************************************************************/
  285. static void DumpData(char *FileName,
  286.              IPObjectStruct *NoProcessObjs,
  287.              IPObjectStruct *PObjects)
  288. {
  289.     FILE *f;
  290.  
  291.     if (FileName != NULL) {
  292.     if ((f = fopen(FileName, "w")) == NULL) {
  293.         fprintf(stderr, "Failed to open \"%s\".\n", FileName);
  294.         IllustrateExit(2);
  295.     }
  296.     }
  297.     else
  298.     f = stdout;
  299.  
  300.     fprintf(f, "\tIrit Solid Modeller Data File (ILLUSTRT), %s\n\n",
  301.         IritRealTimeDate());
  302.  
  303.     while (PObjects) {
  304.     /* Dump only polys with at least two vertices and points/vectors. */
  305.     if (IP_IS_POLY_OBJ(PObjects)) {
  306.         if (IP_IS_POLYLINE_OBJ(PObjects))
  307.         CleanUpPolylineList(&PObjects -> U.Pl);
  308.         if (PObjects -> U.Pl != NULL)
  309.         IritPrsrPutObject(f, PObjects);
  310.     }
  311.     else if (IP_IS_POINT_OBJ(PObjects) ||
  312.          IP_IS_VEC_OBJ(PObjects)) {
  313.         IritPrsrPutObject(f, PObjects);
  314.     }
  315.  
  316.     PObjects = PObjects -> Pnext;
  317.     }
  318.  
  319.     while (NoProcessObjs) {
  320.     IritPrsrPutObject(f, NoProcessObjs);
  321.  
  322.     NoProcessObjs = NoProcessObjs -> Pnext;
  323.     }
  324.  
  325.     fclose(f);
  326. }
  327.  
  328. /*****************************************************************************
  329. * Illustrate exit routine.                              *
  330. *****************************************************************************/
  331. void IllustrateExit(int ExitCode)
  332. {
  333.     exit(ExitCode);
  334. }
  335.